package com.tapjoy;

import android.net.Uri;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.util.EntityUtils;

public class TapjoyURLConnection
{
  private static final String TAPJOY_URL_CONNECTION = "TapjoyURLConnection";
  public static final int TYPE_GET = 0;
  public static final int TYPE_POST = 1;

  public String connectToURL(String paramString)
  {
    return connectToURL(paramString, "");
  }

  public String connectToURL(String paramString1, String paramString2)
  {
    String str1 = null;
    try
    {
      String str2 = (paramString1 + paramString2).replaceAll(" ", "%20");
      TapjoyLog.i("TapjoyURLConnection", "baseURL: " + paramString1);
      TapjoyLog.i("TapjoyURLConnection", "requestURL: " + str2);
      HttpURLConnection localHttpURLConnection = (HttpURLConnection)new URL(str2).openConnection();
      localHttpURLConnection.setConnectTimeout(15000);
      localHttpURLConnection.setReadTimeout(30000);
      str1 = localHttpURLConnection.getResponseMessage();
      localHttpURLConnection.connect();
      localBufferedReader = new BufferedReader(new InputStreamReader(localHttpURLConnection.getInputStream()));
    }
    catch (Exception localException2)
    {
      try
      {
        BufferedReader localBufferedReader;
        StringBuilder localStringBuilder = new StringBuilder();
        try
        {
          while (true)
          {
            String str3 = localBufferedReader.readLine();
            if (str3 == null)
              break;
            localStringBuilder.append(str3 + '\n');
          }
        }
        catch (Exception localException1)
        {
        }
        while (true)
        {
          label195: TapjoyLog.e("TapjoyURLConnection", "Exception: " + localException1.toString());
          return str1;
          str1 = localStringBuilder.toString();
          TapjoyLog.i("TapjoyURLConnection", "--------------------");
          TapjoyLog.i("TapjoyURLConnection", "response size: " + str1.length());
          TapjoyLog.i("TapjoyURLConnection", "response: ");
          TapjoyLog.i("TapjoyURLConnection", "" + str1);
          TapjoyLog.i("TapjoyURLConnection", "--------------------");
          return str1;
          localException2 = localException2;
        }
      }
      catch (Exception localException3)
      {
        break label195;
      }
    }
  }

  public String connectToURLwithPOST(String paramString, Hashtable<String, String> paramHashtable1, Hashtable<String, String> paramHashtable2)
  {
    String str1 = null;
    HttpPost localHttpPost;
    ArrayList localArrayList;
    try
    {
      String str2 = paramString.replaceAll(" ", "%20");
      TapjoyLog.i("TapjoyURLConnection", "baseURL: " + paramString);
      TapjoyLog.i("TapjoyURLConnection", "requestURL: " + str2);
      localHttpPost = new HttpPost(str2);
      localArrayList = new ArrayList();
      Iterator localIterator1 = paramHashtable1.entrySet().iterator();
      while (true)
      {
        boolean bool1 = localIterator1.hasNext();
        str1 = null;
        if (!bool1)
          break;
        Map.Entry localEntry1 = (Map.Entry)localIterator1.next();
        localArrayList.add(new BasicNameValuePair((String)localEntry1.getKey(), (String)localEntry1.getValue()));
        TapjoyLog.i("TapjoyURLConnection", "key: " + (String)localEntry1.getKey() + ", value: " + Uri.encode((String)localEntry1.getValue()));
      }
    }
    catch (Exception localException)
    {
      TapjoyLog.e("TapjoyURLConnection", "Exception: " + localException.toString());
      return str1;
    }
    if (paramHashtable2 != null)
    {
      int i = paramHashtable2.size();
      str1 = null;
      if (i > 0)
      {
        Iterator localIterator2 = paramHashtable2.entrySet().iterator();
        while (true)
        {
          boolean bool2 = localIterator2.hasNext();
          str1 = null;
          if (!bool2)
            break;
          Map.Entry localEntry2 = (Map.Entry)localIterator2.next();
          localArrayList.add(new BasicNameValuePair("data[" + (String)localEntry2.getKey() + "]", (String)localEntry2.getValue()));
          TapjoyLog.i("TapjoyURLConnection", "key: " + (String)localEntry2.getKey() + ", value: " + Uri.encode((String)localEntry2.getValue()));
        }
      }
    }
    localHttpPost.setEntity(new UrlEncodedFormEntity(localArrayList));
    TapjoyLog.i("TapjoyURLConnection", "HTTP POST: " + localHttpPost.toString());
    BasicHttpParams localBasicHttpParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(localBasicHttpParams, 15000);
    HttpConnectionParams.setSoTimeout(localBasicHttpParams, 30000);
    HttpResponse localHttpResponse = new DefaultHttpClient(localBasicHttpParams).execute(localHttpPost);
    str1 = EntityUtils.toString(localHttpResponse.getEntity());
    TapjoyLog.i("TapjoyURLConnection", "--------------------");
    TapjoyLog.i("TapjoyURLConnection", "response status: " + localHttpResponse.getStatusLine().getStatusCode());
    TapjoyLog.i("TapjoyURLConnection", "response size: " + str1.length());
    TapjoyLog.i("TapjoyURLConnection", "response: ");
    TapjoyLog.i("TapjoyURLConnection", "" + str1);
    TapjoyLog.i("TapjoyURLConnection", "--------------------");
    return str1;
  }

  public String getContentLength(String paramString)
  {
    try
    {
      String str2 = paramString.replaceAll(" ", "%20");
      TapjoyLog.i("TapjoyURLConnection", "requestURL: " + str2);
      HttpURLConnection localHttpURLConnection = (HttpURLConnection)new URL(str2).openConnection();
      localHttpURLConnection.setConnectTimeout(15000);
      localHttpURLConnection.setReadTimeout(30000);
      String str3 = localHttpURLConnection.getHeaderField("content-length");
      str1 = str3;
      TapjoyLog.i("TapjoyURLConnection", "content-length: " + str1);
      return str1;
    }
    catch (Exception localException)
    {
      while (true)
      {
        TapjoyLog.e("TapjoyURLConnection", "Exception: " + localException.toString());
        String str1 = null;
      }
    }
  }

  public TapjoyHttpURLResponse getResponseFromURL(String paramString1, String paramString2)
  {
    return getResponseFromURL(paramString1, paramString2, 0);
  }

  public TapjoyHttpURLResponse getResponseFromURL(String paramString1, String paramString2, int paramInt)
  {
    TapjoyHttpURLResponse localTapjoyHttpURLResponse = new TapjoyHttpURLResponse();
    HttpURLConnection localHttpURLConnection = null;
    try
    {
      String str2 = (paramString1 + paramString2).replaceAll(" ", "%20");
      TapjoyLog.i("TapjoyURLConnection", "baseURL: " + paramString1);
      TapjoyLog.i("TapjoyURLConnection", "requestURL: " + str2);
      TapjoyLog.i("TapjoyURLConnection", "type: " + paramInt);
      localHttpURLConnection = (HttpURLConnection)new URL(str2).openConnection();
      localHttpURLConnection.setConnectTimeout(15000);
      localHttpURLConnection.setReadTimeout(30000);
      if (paramInt == 1)
        localHttpURLConnection.setRequestMethod("POST");
      localHttpURLConnection.connect();
      localTapjoyHttpURLResponse.statusCode = localHttpURLConnection.getResponseCode();
      localBufferedReader1 = new BufferedReader(new InputStreamReader(localHttpURLConnection.getInputStream()));
    }
    catch (Exception localException2)
    {
      try
      {
        localStringBuilder1 = new StringBuilder();
        try
        {
          while (true)
          {
            String str3 = localBufferedReader1.readLine();
            if (str3 == null)
              break;
            localStringBuilder1.append(str3 + '\n');
          }
        }
        catch (Exception localException1)
        {
        }
        while (true)
        {
          TapjoyLog.e("TapjoyURLConnection", "Exception: " + localException1.toString());
          if (localHttpURLConnection == null)
            break;
          try
          {
            if (localTapjoyHttpURLResponse.response != null)
              break;
            localBufferedReader2 = new BufferedReader(new InputStreamReader(localHttpURLConnection.getErrorStream()));
          }
          catch (Exception localException5)
          {
            try
            {
              BufferedReader localBufferedReader2;
              StringBuilder localStringBuilder2 = new StringBuilder();
              try
              {
                while (true)
                {
                  String str1 = localBufferedReader2.readLine();
                  if (str1 == null)
                    break;
                  localStringBuilder2.append(str1 + '\n');
                }
              }
              catch (Exception localException4)
              {
              }
              while (true)
              {
                label361: TapjoyLog.e("TapjoyURLConnection", "Exception trying to get error code/content: " + localException4.toString());
                while (true)
                {
                  TapjoyLog.i("TapjoyURLConnection", "--------------------");
                  TapjoyLog.i("TapjoyURLConnection", "response status: " + localTapjoyHttpURLResponse.statusCode);
                  TapjoyLog.i("TapjoyURLConnection", "response size: " + localTapjoyHttpURLResponse.contentLength);
                  TapjoyLog.i("TapjoyURLConnection", "response: ");
                  TapjoyLog.i("TapjoyURLConnection", "" + localTapjoyHttpURLResponse.response);
                  TapjoyLog.i("TapjoyURLConnection", "--------------------");
                  return localTapjoyHttpURLResponse;
                  localTapjoyHttpURLResponse.response = localStringBuilder1.toString();
                  String str4 = localHttpURLConnection.getHeaderField("content-length");
                  if (str4 != null);
                  try
                  {
                    localTapjoyHttpURLResponse.contentLength = Integer.valueOf(str4).intValue();
                  }
                  catch (Exception localException7)
                  {
                    while (true)
                      TapjoyLog.e("TapjoyURLConnection", "Exception: " + localException7.toString());
                  }
                  localTapjoyHttpURLResponse.response = localStringBuilder2.toString();
                }
                localException5 = localException5;
              }
            }
            catch (Exception localException6)
            {
              break label361;
            }
          }
          localException2 = localException2;
          localBufferedReader1 = null;
          localStringBuilder1 = null;
        }
      }
      catch (Exception localException3)
      {
        BufferedReader localBufferedReader1;
        StringBuilder localStringBuilder1;
        while (true)
          localStringBuilder1 = null;
      }
    }
  }
}

/* Location:           E:\android\Toy Defense 2_dex2jar.jar
 * Qualified Name:     com.tapjoy.TapjoyURLConnection
 * JD-Core Version:    0.6.2
 */